#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
using T = int;
T rad(); // quick read
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
#define rep(i, s, t, d) for (int i = (s); i <= (t); i += (d))
const int max_size = 5 + 100;
const int maxn = 5 + 2e5;

struct Node {
    int v, w;
};

int n;
vector<Node> adj[maxn];
int dep[maxn];
int f[maxn][21]; // f[i][j] = f[f[i][j - 1]][j - 1]
int dis[maxn][21];

void dfs(int u, int p) {
    dep[u] = dep[p] + 1, f[u][0] = p;
    rf(j, 20) f[u][j] = f[f[u][j - 1]][j - 1];
    rf(j, 20) dis[u][j] = min(dis[u][j - 1], dis[f[u][j - 1]][j - 1]);
    for (auto to : adj[u]) {
        if (to.v != p) {
            dis[to.v][0] = to.w;
            dfs(to.v, u);
        }
    }
}

int lca(int u, int v) {
    int ret = inf;
    if (dep[u] < dep[v])
        swap(u, v); // keep deep[u] >= deep[v]
    for (int tmp = dep[u] - dep[v], j = 0; tmp > 0; ++j, tmp >>= 1) {
        if (tmp & 1) ret = min(ret, dis[u][j]), u = f[u][j]; // u move on
    }
    if (u == v) return ret;
    for (int i = 20; i >= 0; --i) {
        if (f[u][i] != f[v][i]) {
            ret = min(ret, dis[u][i]), u = f[u][i];
            ret = min(ret, dis[v][i]), v = f[v][i];
        }
    }
    return min(ret, min(dis[u][0], dis[v][0]));
}

int main() {
    n = rad();
    rf(i, n - 1) {
        int u = rad(), v = rad(), w = rad();
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    dfs(1, 0);
    int q = rad();
    rf(i, q) {
        int u = rad(), v = rad();
        printf("%d\n", lca(u, v));
    }
}

T rad() {
    T back = 0;
    int ch = 0, posi = 0;
    for (; ch < '0' || ch > '9'; ch = getchar())
        posi = ch ^ '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        back = (back << 1) + (back << 3) + (ch & 0xf);
    return posi ? back : -back;
}